package com.eregg.learn.netty.demo;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;

public class ByteBufTest {
    public static void main(String[] args) {
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer(10, 100);
        byteBuf.writeBytes(new byte[]{1,2,3,4,5});
        print("byteBuf.writeBytes(new byte[]{1,2,3,4,5});", byteBuf);

        byteBuf.writeInt(20);
        print("byteBuf.writeInt(20);", byteBuf);

        byteBuf.writeBytes(new byte[]{1,2,3,4,5});
        print("byteBuf.writeBytes(new byte[]{1,2,3,4,5});", byteBuf);


        byte [] bytes = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(bytes);
        print("readBytes(" + bytes.length + ")", byteBuf);

    }

    public static void print(String action, ByteBuf byteBuf){
        System.out.println("after ===========" + action + "============");
        System.out.println("capacity:"+byteBuf.capacity());
        System.out.println("maxCapacity:"+byteBuf.maxCapacity());
        System.out.println("readerIndex:"+byteBuf.readerIndex());
        System.out.println("readableBytes:"+byteBuf.readableBytes());
        System.out.println("isReadable:"+byteBuf.isReadable());
        System.out.println("writerIndex:"+byteBuf.writerIndex());
        System.out.println("writableBytes:"+byteBuf.writableBytes());
        System.out.println("isWritable:"+byteBuf.isWritable());
        System.out.println("maxWritableBytes:"+byteBuf.maxWritableBytes());
        System.out.println();
    }
}
